package org.br.erp.base.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.br.erp.base.entity.*;
import org.br.erp.base.mapper.*;
import org.br.erp.base.service.IScmpartnerService;
import org.br.erp.base.utils.ERPUtils;
import org.br.erp.base.utils.SpringUtils;
import org.br.erp.base.vo.ScmPartnerVO;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 往来单位
 * @Author: jeecg-boot
 * @Date:   2022-08-20
 * @Version: V1.0
 */
@Service
public class ScmpartnerServiceImpl extends ServiceImpl<ScmpartnerMapper, Scmpartner> implements IScmpartnerService {
    @Autowired
    private ScmpartnerMapper scmpartnerMapper;

    @Autowired
    private ScmpartnerbaseMapper scmpartnerbaseMapper;
    @Autowired
    private ScmpartnerlinkmanMapper scmpartnerlinkmanMapper;
    @Autowired
    private ScmpartneraddressitemMapper scmpartneraddressitemMapper;
    @Autowired
    private ScmpartnerattachMapper scmpartnerattachMapper;


    @Autowired
    private SpringUtils springUtils;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMain(Scmpartner scmpartner, List<Scmpartnerbase> scmpartnerbaseList, List<Scmpartnerlinkman> scmpartnerlinkmanList, List<Scmpartneraddressitem> scmpartneraddressitemList, List<Scmpartnerattach> scmpartnerattachList) {
        
        //判断供应商/客户编号是否重复
        String partnercode = scmpartner.getPartnercode();
        QueryWrapper<Scmpartner> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("partnercode",partnercode);
        queryWrapper.eq("partnertype",scmpartner.getPartnertype());
        List<Scmpartner> scmpartners = scmpartnerMapper.selectList(queryWrapper);
        if(scmpartners.size()>0){
            throw new RuntimeException(scmpartner.getPartnercode()+"已存在，不允许重复录入");
        }
        scmpartnerMapper.insert(scmpartner);
        if(scmpartnerbaseList!=null && scmpartnerbaseList.size()>0) {
            for(Scmpartnerbase entity:scmpartnerbaseList) {
                //外键设置
                entity.setParentid(scmpartner.getId());
                scmpartnerbaseMapper.insert(entity);
            }
        }
        if(scmpartnerlinkmanList!=null && scmpartnerlinkmanList.size()>0) {
            int i=0;
            for(Scmpartnerlinkman entity:scmpartnerlinkmanList) {
                //外键设置

                if(ERPUtils.isNoneOrEmpty(entity.getLinkmancode())){
                    continue;
                }

//                entity.setLinkmancode(scmpartner.getPartnercode()+"-"+String.format("%3d",i));
                entity.setParentid(scmpartner.getId());

                scmpartnerlinkmanMapper.insert(entity);
            }
        }
        if(scmpartneraddressitemList!=null && scmpartneraddressitemList.size()>0) {
            for(Scmpartneraddressitem entity:scmpartneraddressitemList) {
                //外键设置
                entity.setParentid(scmpartner.getId());
                if(ERPUtils.isNoneOrEmpty(entity.getAddressname())){
                    continue;
                }
                scmpartneraddressitemMapper.insert(entity);
            }
        }
        if(scmpartnerattachList!=null && scmpartnerattachList.size()>0) {
            for(Scmpartnerattach entity:scmpartnerattachList) {
                //外键设置
                entity.setParentid(scmpartner.getId());
                scmpartnerattachMapper.insert(entity);
            }
        }
        if(scmpartner.getFromid()!=null){
            String fromid = scmpartner.getFromid();
            HashMap<String ,Object> param=new HashMap<>();
            param.put("id",fromid);
            springUtils.invokeServiceNoException("crmcustomerarchiveServiceImpl","updateTopartnerflag",param);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMain(Scmpartner scmpartner,List<Scmpartnerbase> scmpartnerbaseList,List<Scmpartnerlinkman> scmpartnerlinkmanList,List<Scmpartneraddressitem> scmpartneraddressitemList,List<Scmpartnerattach> scmpartnerattachList) {
        scmpartnerMapper.updateById(scmpartner);

        //1.先删除子表数据
        scmpartnerbaseMapper.deleteByMainId(scmpartner.getId());
        scmpartnerlinkmanMapper.deleteByMainId(scmpartner.getId());
        scmpartneraddressitemMapper.deleteByMainId(scmpartner.getId());
        scmpartnerattachMapper.deleteByMainId(scmpartner.getId());

        //2.子表数据重新插入
        if(scmpartnerbaseList!=null && scmpartnerbaseList.size()>0) {
            for(Scmpartnerbase entity:scmpartnerbaseList) {
                //外键设置
                entity.setParentid(scmpartner.getId());
                scmpartnerbaseMapper.insert(entity);
            }
        }
        if(scmpartnerlinkmanList!=null && scmpartnerlinkmanList.size()>0) {
            for(Scmpartnerlinkman entity:scmpartnerlinkmanList) {
                //外键设置
                entity.setParentid(scmpartner.getId());
                scmpartnerlinkmanMapper.insert(entity);
            }
        }
        if(scmpartneraddressitemList!=null && scmpartneraddressitemList.size()>0) {
            for(Scmpartneraddressitem entity:scmpartneraddressitemList) {
                //外键设置
                entity.setParentid(scmpartner.getId());
                scmpartneraddressitemMapper.insert(entity);
            }
        }
        if(scmpartnerattachList!=null && scmpartnerattachList.size()>0) {
            for(Scmpartnerattach entity:scmpartnerattachList) {
                //外键设置
                entity.setParentid(scmpartner.getId());
                scmpartnerattachMapper.insert(entity);
            }
        }
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delMain(String id) {
        scmpartneraddressitemMapper.deleteByMainId(id);
        scmpartnerMapper.deleteById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for(Serializable id:idList) {
            scmpartneraddressitemMapper.deleteByMainId(id.toString());
            scmpartnerMapper.deleteById(id);
        }
    }


    /**
     * 0为供应商，1为客户
     * @return
     */
    @Override
    public List<Scmpartner> getPartnerList(String partnertype) {

        QueryWrapper<Scmpartner> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("partnertype",partnertype);
        List<Scmpartner> scmpartners = scmpartnerMapper.selectList(queryWrapper);
        return scmpartners;
    }

    @Override
    public List<ScmPartnerVO> getPartnerVoList(IPage<ScmPartnerVO> page, QueryWrapper<ScmPartnerVO> queryWrapper) {
        List<ScmPartnerVO> scmpartnerVo = scmpartnerMapper.getScmpartnerVo(page, queryWrapper);
        return scmpartnerVo;
    }

//    @Override
//    public List<ScmPartnerVO> getPartnerVoList(String partnertype) {
//        return null;
//    }

    @Override
    public Scmpartner getPartnerByName(String partnername) {
        QueryWrapper<Scmpartner> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("partnername",partnername);
        List<Scmpartner> scmpartners = scmpartnerMapper.selectList(queryWrapper);
        if(scmpartners.size()>0){
            return scmpartners.get(0);
        }else{
            return null;
        }
    }
    
    @Transactional
    public void updateLinkMan(List<Scmpartnerlinkman> scmpartnerlinkmanList){
        Scmpartnerlinkman linkmanitem = scmpartnerlinkmanList.get(0);
        scmpartnerlinkmanMapper.deleteByMainId(linkmanitem.getParentid());
        for (Scmpartnerlinkman scmpartnerlinkman : scmpartnerlinkmanList) {
            scmpartnerlinkmanMapper.insert(scmpartnerlinkman);
        }
    }

    @Override
    public void updateAddress(List<Scmpartneraddressitem> scmpartneraddressitemList) {
        Scmpartneraddressitem linkmanitem = scmpartneraddressitemList.get(0);
        scmpartneraddressitemMapper.deleteByMainId(linkmanitem.getParentid());
        for (Scmpartneraddressitem scmpartnerlinkman : scmpartneraddressitemList) {
            scmpartneraddressitemMapper.insert(scmpartnerlinkman);
        }
    }

}
